﻿using System.Reflection;
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;

[assembly: ExtensionApplication(typeof(AutoCADLoader.Loader))]

namespace AutoCADLoader
{
    public class Loader : IExtensionApplication
    {
        public void Initialize()
        {
            // 初始加载插件
            LoadPlugin();

            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
                "\n插件已加载，输入RELOADPLUGIN手动重载");
        }

        public static void LoadPlugin()
        {
            try
            {
                var assembly = Assembly.LoadFrom(
                    @"D:\software\VS2022\AutoCADSolution\bin\Entry.dll");

                // 触发初始化方法
                var initMethod = assembly.GetType("Entry.Commands")
                    .GetMethod("Initialize");
                initMethod?.Invoke(null, null);
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n加载失败: {ex.Message}");
            }
        }

        public void Terminate() { }
    }
}
